package com.lmwyc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmwyc.bean.CardList;
import com.lmwyc.bean.UserSession;
import com.lmwyc.service.IEmployeeService;
import com.lmwyc.service.IPunshService;
import com.lmwyc.util.Utils;

@Controller
public class PunshController {
	@Autowired
	private IPunshService punshService;
	@Autowired
	private IEmployeeService employeeService;

	/**
	 * 手动触发所有打卡-按照打卡时间等规则
	 * 
	 * @return
	 */
	@RequestMapping(value = "/punsh")
	@ResponseBody
	public String punsh() {
		punshService.punsh();
		return "OK";
	}
	
	/**
	 * 手动触发所有打卡-按照打卡时间等规则，但1分钟内随机打卡
	 * 
	 * @return
	 */
	@RequestMapping(value = "/punshCus")
	@ResponseBody
	public String punshCus() {
		punshService.punshCus();
		return "OK";
	}

	/**
	 * 设置是否打卡
	 * 
	 * @param userName
	 * @param inuse
	 * @return
	 */
	@RequestMapping(value = "/setuse/{userName}/{inuse}")
	@ResponseBody
	public String setuse(@PathVariable String userName, @PathVariable int inuse) {
		if (userName != null && userName.trim().length() > 0) {
			punshService.setUserSession(userName, null, inuse);
		}
		return "OK";
	}

	/**
	 * 设置session
	 * 
	 * @param userName
	 * @param sessionid
	 * @return
	 */
	@RequestMapping(value = "/setsession/{userName}/{sessionid}")
	@ResponseBody
	public String setsession(@PathVariable String userName, @PathVariable String sessionid) {
		if (userName != null && userName.trim().length() > 0) {
			punshService.setUserSession(userName, sessionid, 1);
		}
		return "OK";
	}

	/**
	 * 刷新session
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/refreshSession")
	@ResponseBody
	public String refreshSession(Model model) {
		punshService.timingGetSession();
		return "OK";
	}

	/**
	 * 手动立即打卡-不记录日志-国家局
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/punshByHand")
	public String punshByHand(Model model) {
		// UserSession user = punshService.getUserInfoByWorkNum(workNum);
		final double lat = 39.91273;
		final double lon = 116.33752;
		model.addAttribute("grid", Utils.getRandomAxes(lat, lon));
		model.addAttribute("ship_place_ind", 500);
		model.addAttribute("onwership_place", "北京");

		return "punsh/index";
	}

	/**
	 * 手动立即打卡-不记录日志-裕友
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/punshByHandYuYou")
	public String punshByHandYuYou(Model model) {
		final double lat = 39.933807;
		final double lon = 116.27711;
		model.addAttribute("grid", Utils.getRandomAxes(lat, lon));
		model.addAttribute("ship_place_ind", 500);
		model.addAttribute("onwership_place", "北京");

		return "punsh/index";
	}

	/**
	 * 手动立即打卡-不记录日志-西安
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/punshByHandXian")
	public String punshByHandXian(Model model) {
		final double lat = 34.211132;
		final double lon = 108.833885;
		model.addAttribute("grid", Utils.getRandomAxes(lat, lon));
		model.addAttribute("ship_place_ind", 500);
		model.addAttribute("onwership_place", "西安");

		return "punsh/index";
	}
	
	/**
	 * 手动立即打卡-不记录日志-配置地址
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/punshByHandGIS/{workNum}")
	public String punshByHandGIS(@PathVariable String workNum, Model model) {
		UserSession user = employeeService.getUser(workNum);
		model.addAttribute("grid", Utils.getRandomAxes(user.getLatitude(), user.getLongitude()));
		model.addAttribute("ship_place_ind", user.getShipPlaceInd());
		model.addAttribute("onwership_place", user.getOnwershipPlace());
		return "punsh/index";
	}
		
	/**
	 * 手动立即打卡-不记录日志-山东
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("/punshByHandJiNan")
	public String punshByHandJiNan(Model model) {
		final double lat = 36.654804;
		final double lon = 117.12819;
		model.addAttribute("grid", Utils.getRandomAxes(lat, lon));
		model.addAttribute("ship_place_ind", 500);
		model.addAttribute("onwership_place", "济南");

		return "punsh/index";
	}

	/**
	 * 根据工号直接打卡-记录日志
	 * 
	 * @param workNum
	 * @return
	 */
	@RequestMapping("/punshNow/{workNum}")
	@ResponseBody
	public String punshNow(@PathVariable String workNum) {
		return punshService.punshNow(workNum);
	}

	/**
	 * 获取当月打卡记录
	 * 
	 * @param workNum
	 * @return
	 */
	@RequestMapping("/record/{workNum}")
	@ResponseBody
	public CardList getRecord(@PathVariable String workNum) {
		return punshService.getRecord(workNum);
	}

	@RequestMapping("/queryPunshAreaList")
	public String queryPunshAreaList(){
		return "punsh/queryPunshAreaList";
	}
}
